Why in Ruby, a || 1 will throw an error when `a` is undefined, but a = a || 1 will not?
Posted
by Jian Lin
on Stack Overflow
See other posts from Stack Overflow
or by Jian Lin
Published on 2010-06-06T06:54:50Z
Indexed on
2010/06/06
7:02 UTC
Read the original article
Hit count: 356
When a is undefined, then a || 1 will throw an error, but a = a || 1 will not. Isn't that a little bit inconsistent?
irb(main):001:0> a
NameError: undefined local variable or method `a' for main:Object
from (irb):1
from c:/ruby/bin/irb:12:in `<main>'
irb(main):002:0> a || 1
NameError: undefined local variable or method `a' for main:Object
from (irb):2
from c:/ruby/bin/irb:12:in `<main>'
irb(main):003:0> a = a || 1
=> 1
© Stack Overflow or respective owner